// 云函数入口文件
const cloud = require('wx-server-sdk');
const COLLECTION_NAME = 'feedback';

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext();
  const db = cloud.database();

  const record = {
    description: event.info.description,
    imgPath: event.info.imgPath,
    openId: wxContext.OPENID,
    time: new Date()
  };

  return db.collection(COLLECTION_NAME).add({
    data: record
  }).then(res => {
    if (res._id) {
      return {
        code: 200,
        msg: '提交成功！'
      }
    } else {
      return {
        code: 500,
        msg: '提及失败！'
      }
    }
  });
}